package SpotTable;

import SpotList.SpotList;
import ch01Exercise.IString;

public class SpotTable implements ISpotTable {
    int len;
    SpotList data = new SpotList(len);

    @Override
    public boolean isEmpty() {
        return this.len == 0;
    }

    @Override
    public int turnName2Id(IString name) {
        for (int i = 0; i < this.len; i++) {
            if (this.data.getName(i) == name){
                return i;
            }
        }
        return -1;
    }

    @Override
    public IString getName(int id) {
        return this.data.getName(id);
    }

    @Override
    public IString getIntro(int id) {
        return this.data.getIntro(id);
    }

    @Override
    public void set(int id, IString name, IString intro) {
        this.data.setName(id, name);
        this.data.setIntro(id, intro);
    }

    @Override
    public void insert(IString name, IString intro) {
        data.setName(this.len, name);
        data.setIntro(this.len, intro);
    }

    @Override
    public int length() {
        return 0;
    }

    @Override
    public void remove(IString name) {
        for(int i = 0;i <= this.len;i++){
            if(this.data.getName(i) == name){
                for(int j = i; j < this.len - 1; j++){//把i后的数据元素依次前移
                    this.data.set(i, this.data.get(j));
                }
                this.len--;
            }
        }
        System.out.println("it's out of index.");
    }

    @Override
    public void removeAll() {
        this.len = 0;
    }

    @Override
    public SpotList getData() {
        return this.data;
    }
}
